package com.yuejian_06.yuejian_06.activity;

import android.app.AlertDialog;
import android.content.DialogInterface;
import android.content.Intent;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageButton;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

import com.squareup.picasso.Picasso;
import com.yuejian_06.yuejian_06.Listener.MeListener;
import com.yuejian_06.yuejian_06.R;
import com.yuejian_06.yuejian_06.bean.ActivityBean;
import com.yuejian_06.yuejian_06.bean.SuccessBean;
import com.yuejian_06.yuejian_06.bean.UserInfoBean;
import com.yuejian_06.yuejian_06.model.HomeModel;
import com.yuejian_06.yuejian_06.model.MeModel;

import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;

public class SignActivity extends BaseActivity {
    private TextView activity_name,activity_price,user_name,sum_price;
    private EditText num,phonenum,IDcard;
    private Button sign;
    private ImageButton reduce,add,back;

    private String activity_id;
    private double allp;

    @Override
    void initViews() {

    }

    @Override
    void initEvents() {

    }

    @Override
    void initData() {

    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_sign);
        initView();//初始化事件
        initEvent();
        Intent intent = getIntent();
        activity_id = intent.getStringExtra("activity_id");
        HomeModel homeModel = new HomeModel();
        homeModel.getActivityInfo(activity_id,meListener);

        MeModel meModel = new MeModel();
        meModel.getUserInfo(getUser_id(),meListener1);


    }
    private  void initView(){
        activity_name = (TextView) findViewById(R.id.activity_name);
        activity_price = (TextView) findViewById(R.id.activity_price);
        user_name = (TextView) findViewById(R.id.user_name);
        sum_price = (TextView) findViewById(R.id.sum_price);

        IDcard = (EditText) findViewById(R.id.IDcard);
        phonenum = (EditText) findViewById(R.id.phonenum);
        num = (EditText) findViewById(R.id.num);

        reduce = (ImageButton) findViewById(R.id.reduce);
        add = (ImageButton) findViewById(R.id.add);
        back = (ImageButton) findViewById(R.id.back);

        sign = (Button) findViewById(R.id.sign);
    }

    private  void initEvent(){
        back.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                finish();
            }
        });
        reduce.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                String str_num = num.getText().toString();
                if (!"0".equals(str_num)){
                    double price = Double.parseDouble(activity_price.getText().toString());
                    int numint = Integer.parseInt(str_num)-1;
                    allp = price * numint;
                    num.setText(numint+"");
                    sum_price.setText(allp+"");
                }
            }
        });

        add.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                String str_num = num.getText().toString();
                if (!"0".equals(str_num)){
                    double price = Double.parseDouble(activity_price.getText().toString());
                    int numint = Integer.parseInt(str_num)+1;
                    allp = price * numint;
                    num.setText(numint+"");
                    sum_price.setText(allp+"");
                }
            }
        });

        sign.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                AlertDialog.Builder builder = new AlertDialog.Builder(SignActivity.this);
                builder.setIcon(R.drawable.heart_p);
                builder.setTitle(R.string.builder_title);
                builder.setMessage(R.string.builder_message);
                builder.setPositiveButton(R.string.postive_button, new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialog, int which) {
                        String IDcard2 = IDcard.getText().toString();
                        String sum_price2 = sum_price.getText().toString();
                        String phonenum2 = phonenum.getText().toString();
                        String activity_name2 = activity_name.getText().toString();

                        HomeModel homeModel = new HomeModel();
                        homeModel.getAddActivity(getUser_id(),activity_id,activity_name2,IDcard2,sum_price2,phonenum2,meListener2);


                    }
                });

                builder.setNegativeButton(R.string.negative_button, new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialog, int which) {
                        Toast.makeText(SignActivity.this,"已经取消报名",Toast.LENGTH_SHORT).show();
                    }
                });

                builder.setCancelable(false);
                AlertDialog dialog=builder.create();
                dialog.show();

            }
        });


    }


    //活动信息
    private MeListener<ActivityBean> meListener =new MeListener<ActivityBean>() {
        @Override
        public void onResponse(ActivityBean o) {
            ActivityBean res=o;
            activity_name.setText(res.getActivity_name());
            activity_price.setText(res.getActivity_price());
            sum_price.setText(res.getActivity_price());
        }
        @Override
        public void onFail(String msg) {
            Toast.makeText(SignActivity.this,"活动详情失败",Toast.LENGTH_SHORT).show();
        }
    };

    //用户信息
    private MeListener<UserInfoBean> meListener1 =new MeListener<UserInfoBean>() {
        @Override
        public void onResponse(UserInfoBean o) {
            UserInfoBean res=o;
            user_name.setText(res.getUser_name());
            phonenum.setText(res.getPhonenum());
        }
        @Override
        public void onFail(String msg) {
            Toast.makeText(SignActivity.this,"个人详情失败",Toast.LENGTH_SHORT).show();
        }
    };

    //报名
    private MeListener<SuccessBean> meListener2 =new MeListener<SuccessBean>() {
        @Override
        public void onResponse(SuccessBean o) {
            Toast.makeText(SignActivity.this,"报名成功",Toast.LENGTH_SHORT).show();
            Intent intent = new Intent(SignActivity.this,MainActivity.class);
            startActivityForResult(intent,1);
        }
        @Override
        public void onFail(String msg) {
            Toast.makeText(SignActivity.this,"报名失败",Toast.LENGTH_SHORT).show();
        }
    };
}
